package com.hzhanghuan.recyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * item的事件处理器助手
 * Created by Administrator on 2016/5/24.
 */
public class ItemEventHelper {

    private ItemRootViewClickListener itemRootViewClickListener;
    private ItemRootViewLongClickListener itemRootViewLongClickListener;
    private DefaultOnChildAttachStateChangeListener defaultOnChildAttachStateChangeListener;


    /**
     * 设置item单击事件监听器
     *
     * @param itemRootViewClickListener
     */
    public void setItemRootViewClickListener(ItemRootViewClickListener itemRootViewClickListener) {
        this.itemRootViewClickListener = itemRootViewClickListener;
    }


    /**
     * 设置长按监听器
     * @param itemRootViewLongClickListener
     */
    public void setItemRootViewLongClickListener(ItemRootViewLongClickListener itemRootViewLongClickListener) {
        this.itemRootViewLongClickListener = itemRootViewLongClickListener;
    }


    /**
     * 绑定RecyclerView
     * @param recyclerView
     */
    public void setup(final RecyclerView recyclerView) {
        if (defaultOnChildAttachStateChangeListener == null) {
            defaultOnChildAttachStateChangeListener = new DefaultOnChildAttachStateChangeListener();
        }
        recyclerView.addOnChildAttachStateChangeListener(defaultOnChildAttachStateChangeListener);
        recyclerView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
                if (defaultOnChildAttachStateChangeListener != null) {
                    recyclerView.addOnChildAttachStateChangeListener(defaultOnChildAttachStateChangeListener);
                }
            }


            @Override
            public void onViewDetachedFromWindow(View v) {
                recyclerView.removeOnChildAttachStateChangeListener(defaultOnChildAttachStateChangeListener);
            }
        });
    }



    /**
     * itemView单击监听器
     */
    public interface ItemRootViewClickListener {
        void onItemClick(RecyclerView.ViewHolder viewHolder);
    }


    /**
     * itemView长按监听器
     */
    public interface ItemRootViewLongClickListener {
        boolean onItemLongClick(RecyclerView.ViewHolder viewHolder);
    }


    class DefaultOnChildAttachStateChangeListener implements RecyclerView.OnChildAttachStateChangeListener {


        @Override
        public void onChildViewAttachedToWindow(View view) {
            DefaultItemViewListener listener = new DefaultItemViewListener();
            view.setOnClickListener(listener);
            view.setOnLongClickListener(listener);
        }


        @Override
        public void onChildViewDetachedFromWindow(View view) {
            view.setOnClickListener(null);
            view.setOnLongClickListener(null);
        }
    }


    class DefaultItemViewListener implements View.OnClickListener, View.OnLongClickListener {


        @Override
        public void onClick(View v) {
            RecyclerView recyclerView = (RecyclerView) v.getParent();
            RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(v);
            if (itemRootViewClickListener != null) {
                itemRootViewClickListener.onItemClick(viewHolder);
            }
        }


        @Override
        public boolean onLongClick(View v) {
            RecyclerView recyclerView = (RecyclerView) v.getParent();
            RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(v);
            if (itemRootViewLongClickListener != null) {
                return itemRootViewLongClickListener.onItemLongClick(viewHolder);
            }
            return false;
        }
    }


}
